<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>shell 练习-自动重启php-fpm | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>shell 练习-自动重启php-fpm</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/shell练习/">shell练习</a>
			</span>
		
	</div>

	

	
		<p>服务器上跑的是LNMP环境，近期总是有502现象。502为网站访问的状态码，200正常，502错误是nginx最为普通的错误状态码。由于502只是暂时的，并且只要一重启php-fpm服务则502消失，但不重启的话，则会一直持续很长时间。所以有必要写一个监控脚本，监控访问日志的状态码，一旦发生502，则自动重启一下php-fpm。</p>
<p>我们设定： </p>
<ol>
<li>access_log  /data1/log/access.log</li>
<li>脚本死循环，每10s检测一次 （假设每10s钟的日志条数为300左右）</li>
<li>重启php-fpm的方法是  /etc/init.d/php-fpm restart</li>
</ol>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#! /bin/bash</span></div><div class="line"><span class="built_in">log</span>=/data1/<span class="built_in">log</span>/access.log</div><div class="line">N=10</div><div class="line"><span class="keyword">while</span> :; <span class="keyword">do</span></div><div class="line">    <span class="comment">##因为10秒钟大概产生300条日志</span></div><div class="line">    tail -n 300 <span class="variable">$log</span> &gt; /tmp/<span class="built_in">log</span></div><div class="line">    n_502=`grep -c <span class="string">' 502"'</span> /tmp/<span class="built_in">log</span>`</div><div class="line">    <span class="keyword">if</span> [ <span class="variable">$n_502</span> -ge <span class="variable">$N</span> ]; <span class="keyword">then</span></div><div class="line">        <span class="comment">##记录系统的状态</span></div><div class="line">        top -bn1 &amp;gt;/tmp/`date +%H%M%S`-top.log</div><div class="line">        vmstat 1 5 &amp;gt;/tmp/`date +%H%M%S`-vm.log</div><div class="line">        /etc/init.d/php-fpm restart 2&amp;gt; /dev/null</div><div class="line">        <span class="comment">##重启php-fpm服务后，应先暂缓1分钟，而后继续每隔10s检测一次</span></div><div class="line">        sleep 60</div><div class="line">    <span class="keyword">fi</span></div><div class="line">    sleep 10</div><div class="line"><span class="keyword">done</span></div></pre></td></tr></table></figure>
	

	
		<span class="different-posts"><a href="/2017/10/12/Shell 练习/11. shell 练习-自动重启php-fpm/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
